package com.mylg.craw.house.lbs.gaode.bean;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.mylg.craw.house.util.DistanceUtils;
import com.mylg.craw.house.util.JacksonUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
@ToString
@JsonNaming(PropertyNamingStrategies.LowerCaseStrategy.class)
public class GeoCodeRes extends GaoDeListResponse {
    private List<GeoCodeItem> geoCodes;


    public String computeLocation() {
        if (CollectionUtils.isEmpty(geoCodes)) {
            return null;
        }
        String first = geoCodes.get(0).getLocation();
        if (geoCodes.size() == 1) {
            return first;
        }

        List<String> locations = new ArrayList<>();
        for (GeoCodeItem item : geoCodes) {
            if (StringUtils.isEmpty(item.getLocation())) {
                continue;
            }

            if (item.getLocation().equals(first)) {
                continue;
            }

            double distance = DistanceUtils.calculateDistance(first, item.getLocation());
            if (distance < 3) {
                continue;
            }

            locations.add(item.getLocation());
        }

        if (!locations.isEmpty()) {
            System.out.println("距离相差较大;json=" + JacksonUtils.writeValueAsString(geoCodes));
        }
        return first;
    }
}
